package com.vess.ratingweb;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class RatingPage extends Activity {

	Button btnNext;
	WebView webView;
	private String mStrUsername, mStrURL;
	private String mStrTimeStamp, mStrLogo, mStrCoupon;
	private int mIntUserId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rating_page);
		webView = (WebView) findViewById(R.id.webRating);
		btnNext = (Button) findViewById(R.id.btnNext);

		mStrUsername = getIntent().getStringExtra("username");
		mStrTimeStamp = getIntent().getStringExtra("timestamp");
		mStrLogo = getIntent().getStringExtra("logo");
		mStrURL = getIntent().getExtras().getString("url");
		mStrCoupon = getIntent().getStringExtra("coupon");
		mIntUserId = getIntent().getIntExtra("id", 0);

		WebSettings webSettings = webView.getSettings();
		webSettings.setBuiltInZoomControls(true);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setUserAgentString("Chrome");
		webView.setWebViewClient(new Callback(this, mStrURL)); // HERE IS THE MAIN CHANGE
		webView.loadUrl(mStrURL);
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(RatingPage.this, CallBack.class);
				intent.putExtra("url", mStrURL);
				intent.putExtra("username", mStrUsername);
				intent.putExtra("timestamp", mStrTimeStamp);
				intent.putExtra("logo", mStrLogo);
				intent.putExtra("coupon", mStrCoupon);
				intent.putExtra("id", mIntUserId);
				startActivity(intent);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.d("ratpageresult", "passed");
		if (resultCode == RESULT_OK) {
			Log.d("ratpageresult", "passed");
			Log.d("ratpageresult", data.getExtras().getString("url"));
			// webView.setWebViewClient(new WebviewCookie());
			// webView.loadUrl(data.getExtras().getString("url"));
		}
	}

	private class Callback extends WebViewClient { // HERE IS THE MAIN CHANGE.

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return (false);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			proDialog.dismiss();
			view.setVisibility(View.VISIBLE);
			CookieSyncManager.getInstance().sync();
			// Log.d("webviewurl", webView.getUrl());
			// Log.d("opendiretory", webView.getUrl());
			if (webView.getUrl().toLowerCase()
					.contains("returnurl=&source=gplus".toLowerCase())) {

				Log.d("opendiretory", webView.getUrl());
				webView.loadUrl("http://www.dealerrater.com/dealer/Pi-Motorsports-Inc-review-10827/");
				// webView.setWebViewClient(new WebviewCookie());

			}
		}

		private Context mCtx;
		private ProgressDialog proDialog;
		
		public Callback(Context ctx, String strURL) {
			mCtx = ctx;
			proDialog = new ProgressDialog(mCtx);
			proDialog.setTitle(strURL);
			proDialog.setMessage("Loading...");
			proDialog.setCanceledOnTouchOutside(false);
			proDialog.show();
		}
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			view.setVisibility(View.GONE);
		}
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, Directory.class);
		intent.putExtra("username", mStrUsername);
		intent.putExtra("timestamp", mStrTimeStamp);
		intent.putExtra("logo", mStrLogo);
		intent.putExtra("coupon", mStrCoupon);
		intent.putExtra("id", mIntUserId);
		startActivity(intent);
		finish();
		super.onBackPressed();
	}

}
